package cn.fulong.smebj.admin.service.impl;

import cn.fulong.frame.config.Result;
import cn.fulong.smebj.admin.service.ContestService;
import cn.fulong.smebj.entity.User;
import cn.fulong.smebj.mapper.UserMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ContestServiceImpl implements ContestService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Result<Object> addInformation(User user) {
        try {
            User originUser = userMapper.selectById(user.getId());
            user.setUserName(originUser.getUserName());
            user.setPassword(originUser.getPassword());
            user.setCreateTime(originUser.getCreateTime());
            user.setRoleId(originUser.getRoleId());

            user.setStatus("2");
            userMapper.updateById(user);
            return Result.success();
        }catch (Exception e){
            return Result.failure(e.toString());
        }

    }

    @Override
    public Result<Object> myInformation(Long id) {
        User user = userMapper.selectById(id);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.failure("系统错误，用户信息不存在");
        }
    }

    @Override
    public Result<Object> infoList(Integer pageNo, Integer pageSize) {
        IPage<Map<String,Object>> iPage = new Page<>(Optional.ofNullable(pageNo).orElse(0), pageSize);
        IPage<Map<String,Object>> users = userMapper.selectAll(iPage);
        return Result.success(users);
    }
}
